/*
 * Window.cpp
 *
 *  Created on: 03-02-2012
 *      Author: glu
 */

#include "Window.h"

Window::Window(): SCREEN_WIDTH(800), SCREEN_HEIGHT(600), SCREEN_BPP(32){
	state = NULL;

	gameState = Battle;
	nextState = NoState;
}

void Window::clean(){
	delete state;
	SDL_Quit();
}

bool Window::init(){
	state = new BattleState();
	if(state == NULL)
		return false;

	if (SDL_Init(SDL_INIT_VIDEO) == -1)
		return false;
	Global::screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
	if( Global::screen == NULL )
		return false;
	SDL_WM_SetCaption("Battle of Heroes", NULL);

	return true;
}

bool Window::run(){
	state->update();
	while( gameState != ExitState ){
		while( SDL_PollEvent(&event) ){
			state->handleEvents(event);
		}

//		Miejsce na logike gry, np przesuwanie postaci, atakowanie
		state->logic();

		changeState();
		state->update();

		if (SDL_Flip(Global::screen) == -1)
			return false;
		SDL_Delay(100);
	}
	return true;
}

void Window::changeState(){
	nextState = state!=NULL ? state->getNextState() : NoState;

	if (nextState != NoState){

		if(nextState != ExitState)
			delete state;

		switch(nextState){
		case Intro: {
			break;
		}
		case Menu: {
			break;
		}
		case Battle: {
			state = new BattleState();
			break;
		}
		default: break;
		}

		gameState = nextState;
		nextState = NoState;
	}
}
